/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.citi.dahg.cve;

import com.citi.dahg.cve.commons.ServiceBean;
import com.citi.dahg.cve.controller.model.Area;
import com.citi.dahg.cve.controller.model.Pais;
import com.citi.dahg.cve.controller.model.Recurso;
import com.citi.dahg.cve.controller.service.Service;
import com.citi.dahg.cve.controller.service.local.AreaService;
import com.citi.dahg.cve.controller.service.local.RecursoService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

/**
 *
 * @author dh90940
 */
@ManagedBean
@ViewScoped
public class BusquedaEmpleadosBean extends BasicManagedBean implements ServiceBean<Recurso> {
    
    @EJB
    private AreaService areaService;    
    private TreeNode areas;
    
    private TreeNode selectedNode;
    private List<Recurso> empleados;

    @PostConstruct
    public void init() {
        empleados = new ArrayList<Recurso>();
        areas = new DefaultTreeNode("root", "root", null);
        List<Pais> paisesList = areaService.getAllPaises();
        
        for(Pais p: paisesList) {
            TreeNode nodePais= new DefaultTreeNode("pais", p, areas);
            List<Area> areasList = areaService.getAreaByPais(p);
            for(Area a:areasList) {                
                TreeNode nodeArea=new DefaultTreeNode("area", a, nodePais);
                List<Recurso> supervisoresList = areaService.getSupervisores(a);
                for(Recurso s:supervisoresList) {
                    TreeNode nodeSupervisor = new DefaultTreeNode("supervisor", s, nodeArea);
                }
            }
        }
        
    }
    
    public TreeNode getAreas() {
        return areas;
    }

    public void setAreas(TreeNode areas) {
        this.areas = areas;
    }

    public TreeNode getSelectedNode() {
        return selectedNode;
    }

    public void setSelectedNode(TreeNode selectedNode) {
        this.selectedNode = selectedNode;
    }

    public void mostrar() {
        
        if (selectedNode==null) {
            addWarning("Debe seleccionar un supervisor");
            return;
        }
        
        String type=selectedNode.getType();
        empleados.clear();
        if(type.equals("supervisor")) {
            Recurso supervisor = (Recurso) selectedNode.getData();
            List<Recurso> subAlternos = areaService.getSubalternos(supervisor);
            
            if(subAlternos.isEmpty()) {
                addInfo("No hay subalternos para el supervisor seleccionado");
                return ;
            }
            
            empleados.addAll(subAlternos);
        }
        else addWarning("Debe seleccionar un supervisor");
    }

    public List<Recurso> getEmpleados() {
        return empleados;
    }

    public void setEmpleados(List<Recurso> empleados) {
        this.empleados = empleados;
    }
    
    public List<Recurso> completeSoeid(String query) {
        List<Recurso> lista = new ArrayList<Recurso>();
        lista.addAll(recursoService.findLikeSoeid(query));
        return lista;
    }
    
    public List<Recurso> completeName(String query) {
        List<Recurso> lista = new ArrayList<Recurso>();
        lista.addAll(recursoService.findLikeName(query));
        return lista;
    }
    
    public List<Recurso> completeLastName(String query) {
        List<Recurso> lista = new ArrayList<Recurso>();
        lista.addAll(recursoService.findLikeLastName(query));
        return lista;
    }

    @EJB
    private RecursoService recursoService;
    
    @Override    
    public Service<Recurso> getService() {
        return recursoService;
    }
    
    public void handleSelect(SelectEvent event) {
        empleados.clear();
        Recurso r = (Recurso) event.getObject();
        empleados.add(r);
    }
}
